package com.roy.ribbon;

import com.netflix.client.ClientFactory;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.netflix.loadbalancer.Server;

public class HttpRibbonMain {
	public static void main(String[] args) {
		/**
		 * 设置服务器列表。和配置到properties里一样
		 * ribbon的配置分两部分，一部分是Ribbon的通用配置。以ribbon.开头。
		 * 另一部分是各实例的配置。<instanceId>.ribbon.<key>=value
		 * 可选key:
		 * NFLoadBalancerClassName：应配置ILoadBalancer
		 * NFLoadBalancerRuleClassName：应配置IRule
		 * NFLoadBalancerPingClassName：应配置IPing
		 * NIWSServerListClassName：应配置ServerList
		 * NIWSServerListFilterClassName应配置ServerListFilter
		 * listOfServers 配置服务器列表(脱离Eureka)
		 */
		//ribbon的配置
		ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers",
				"localhost:8081,localhost:8082,localhost:8083,localhost:8084");
		//该实例是ZoneAwareLoadBalancer 实例
		BaseLoadBalancer lb = (BaseLoadBalancer)ClientFactory.getNamedLoadBalancer("my-client");
		//自定义负载均衡规则
		IRule rule = new RoundRobinRule();
//		IRule rule = new BestAvailableRule();
//		IRule rule = new AvailabilityFilteringRule();
//		IRule rule = new RandomRule();
//		IRule rule = new WeightedResponseTimeRule();
//		IRule rule = new RetryRule();
//		IRule rule = new MyRule();
		
		lb.setRule(rule);
		for(int i = 0 ; i < 10 ; i ++) {
			Server server = lb.chooseServer(null);
			System.out.println(server);
		}
	}
}
